<template>
    <div class="reg-wrap">
        <div class="ms-reg">
            <div class="ms-title">添加运单</div>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="发货单号" prop="order_number">
                    <el-input type="text" v-model="ruleForm.order_number" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="客户信息" prop="client_id">
                    <el-select v-model="ruleForm.client_id" clearable placeholder="请选择">
                        <el-option v-for="item in client_list" :key="item.id" :label="item.client_name" :value="item.id"> </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="设备id" prop="tag_id">
                    <el-select v-model="ruleForm.tag_id" clearable placeholder="请选择">
                        <el-option v-for="item in tag_list" :key="item.id" :label="item.tag_id" :value="item.tag_id"> </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')" v-if="!this.tid">添加</el-button>
                    <el-button type="primary" @click="update('ruleForm')" v-else>编辑完成</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { add_order, client, tags } from '../../api/api.js';
export default {
    name: 'login',
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入发货单号'));
            } else {
                if (this.ruleForm.password !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请选择客户信息'));
            } else {
                callback();
            }
        };
        var validatePass3 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请选择设备id'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                order_number: '',
                client_id: '',
                tag_id: ''
            },
            rules: {
                order_number: [{ validator: validatePass, trigger: 'blur' }],
                client_id: [{ validator: validatePass2, trigger: 'blur' }],
                tag_id: [{ validator: validatePass3, trigger: 'blur' }]
            },
            tid: '',
            client_list: [],
            tag_list: []
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    add_order({
                        order_number: this.ruleForm.order_number,
                        client_id: this.ruleForm.client_id,
                        tag_id: this.ruleForm.tag_id
                    }).then(res => {
                        if (res.data.code == 200) {
                            this.$message(res.data.msg);
                            this.$router.push('/order_list')
                        } else {
                            console.log(res);
                        }
                    });
                } else {
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        // update(formName) {
        //     this.$refs[formName].validate(valid => {
        //         if (valid) {
        //             if (this.tid) {
        //                 put_tags({
        //                     tid: this.tid,
        //                     order_number: this.ruleForm.order_number,
        //                     client_id: this.ruleForm.client_id
        //                 }).then(res => {
        //                     this.$message(res.data.msg);
        //                 });
        //             }
        //         } else {
        //             return false;
        //         }
        //     });
        // },
        // 获取所有客户信息
        get_cate() {
            client().then(res => {
                this.client_list = res.data.data;
            });
        },
        // 生成随机发货单号
        code: function() {
            var pasArr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
            var code = '';
            var pasArrLen = pasArr.length;
            for (var i = 0; i < 11; i++) {
                var x = Math.floor(Math.random() * pasArrLen);
                code += pasArr[x];
            }
            this.ruleForm.order_number = code;
            this.$message('已为您自动生成发货单号');
        },
        // 获取所有标签
        get_tags() {
            tags().then(res => {
                this.tag_list = res.data.data;
            });
        }
    },
    mounted() {
        this.get_cate();
        this.get_tags();
        if (!this.tid) {
            this.code();
        } else {
            this.tid = this.$route.query.tid;
            this.ruleForm.tag_id = this.$route.query.tag_id;
        }
    }
};
</script>

<style scoped>
.reg-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-reg {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.reg-btn {
    text-align: center;
}
.reg-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.reg-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>